/*****************************************************************************
*        描述 :
*             文件描述
*        CLR版本:            3.5
*        创建者 :            蒋 光
*        个人域名:           jiangguang.net.cn
*        命名空间名称:       ECP.DataAccess
*        文件名:             BaseRoleDao.cs
*        创建系统时间:       2012/11/2 19:07:00
*        创建年份:           2012
*
/*****************************************************************************/

namespace ECP.DataAccess
{
    using System.Data;
    using ECP.DataAccess.Utilities;
    using ECP.Model;
    using ECP.Utilities;

    public class BaseRoleDao : BaseDao
    {
        private static BaseRoleDao instance = null;
        private static object locker = new object();

        public BaseRoleDao()
        {
            base.CurrentTableName = BaseRoleTable.TableName;
        }

        public BaseRoleDao(IDbHelper dbHelper)
            : this()
        {
            base.DbHelper = dbHelper;
        }

        public BaseRoleDao(BaseUserInfo userInfo)
            : this()
        {
            base.UserInfo = userInfo;
        }

        public BaseRoleDao(IDbHelper dbHelper, BaseUserInfo userInfo)
            : this(dbHelper)
        {
            base.UserInfo = userInfo;
        }

        public string Add(BaseRoleEntity roleEntity, out string statusCode)
        {
            string str = string.Empty;
            if (this.Exists(BaseRoleTable.FieldOrganizeID, roleEntity.OrganizeID, BaseRoleTable.FieldRealname, roleEntity.Realname))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return str;
            }
            str = this.AddEntity(roleEntity);
            statusCode = StatusCode.OKAdd.ToString();
            return str;
        }

        public string AddEntity(BaseRoleEntity roleEntity)
        {
            string sequence = BaseSequenceDao.Instance.GetSequence(base.DbHelper, BaseRoleTable.TableName);
            roleEntity.SortCode = sequence;
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginInsert(BaseRoleTable.TableName);
            sqlBuilder.SetValue(BaseRoleTable.FieldID, sequence);
            this.SetEntity(sqlBuilder, roleEntity);
            sqlBuilder.SetValue(BaseRoleTable.FieldCreateUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseRoleTable.FieldCreateDate);
            if (sqlBuilder.EndInsert() <= 0)
            {
                return string.Empty;
            }
            return sequence;
        }

        public override int BatchDelete(string[] ids)
        {
            int num = 0;
            for (int i = 0; i < ids.Length; i++)
            {
                num += this.Delete(ids[i]);
            }
            return num;
        }

        public int BatchMoveTo(string[] ids, string targetID)
        {
            int num = 0;
            for (int i = 0; i < ids.Length; i++)
            {
                num += this.MoveTo(ids[i], targetID);
            }
            return num;
        }

        public override int BatchSave(DataTable dataTable)
        {
            int num = 0;
            BaseRoleEntity roleEntity = new BaseRoleEntity();
            foreach (DataRow row in dataTable.Rows)
            {
                if (row.RowState == DataRowState.Deleted)
                {
                    string id = row[BaseRoleTable.FieldID, DataRowVersion.Original].ToString();
                    if (id.Length > 0)
                    {
                        num += this.Delete(id);
                    }
                }
                if ((row.RowState == DataRowState.Modified) && !string.IsNullOrEmpty(row[BaseRoleTable.FieldID, DataRowVersion.Original].ToString()))
                {
                    roleEntity.GetFrom(row);
                    num += this.UpdateEntity(roleEntity);
                }
                if (row.RowState == DataRowState.Added)
                {
                    roleEntity.GetFrom(row);
                    num += (this.AddEntity(roleEntity).Length > 0) ? 1 : 0;
                }
                if (row.RowState != DataRowState.Unchanged)
                {
                    DataRowState rowState = row.RowState;
                }
            }
            return num;
        }

        public override int Delete(string id)
        {
            int num = 0;
            num += DbLogic.Delete(base.DbHelper, BaseUserRoleTable.TableName, BaseUserRoleTable.FieldRoleID, id);
            return (num + DbLogic.Delete(base.DbHelper, BaseRoleTable.TableName, id));
        }

        public DataTable GetDTByOrganize(string organizeID)
        {
            string sqlQuery = " SELECT " + BaseRoleTable.TableName + ".*, (SELECT COUNT(*) FROM " + BaseUserRoleTable.TableName + " WHERE (Enabled = 1) AND (RoleID = " + BaseRoleTable.TableName + ".ID)) AS StaffCount  FROM " + BaseRoleTable.TableName + " WHERE " + BaseRoleTable.FieldOrganizeID + " = '" + organizeID + "' ORDER BY " + BaseRoleTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseRoleTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        public BaseRoleEntity GetEntity(string id)
        {
            return new BaseRoleEntity(this.Get(id));
        }

        public int MoveTo(string id, string targetID)
        {
            return this.SetProperty(id, BaseRoleTable.FieldOrganizeID, targetID);
        }

        public int ResetSortCode(string organizeID)
        {
            int num = 0;
            DataTable dTByOrganize = this.GetDTByOrganize(organizeID);
            string id = string.Empty;
            string[] strArray = BaseSequenceDao.Instance.GetBatchSequence(base.DbHelper, BaseModuleTable.TableName, dTByOrganize.Rows.Count);
            int index = 0;
            foreach (DataRow row in dTByOrganize.Rows)
            {
                id = row[BaseModuleTable.FieldID].ToString();
                num += this.SetProperty(id, BaseModuleTable.FieldSortCode, strArray[index]);
                index++;
            }
            return num;
        }

        public DataTable Search(string organizeID, string search)
        {
            search = BaseBusinessLogic.GetSearchString(search);
            if (string.IsNullOrEmpty(search))
            {
                if (string.IsNullOrEmpty(organizeID))
                {
                    return this.GetDT();
                }
                return this.GetDTByOrganize(organizeID);
            }
            string sqlQuery = " SELECT " + BaseRoleTable.TableName + ".*, (SELECT COUNT(*) FROM " + BaseUserRoleTable.TableName + " WHERE (Enabled = 1) AND (RoleID = " + BaseRoleTable.TableName + ".ID)) AS StaffCount  FROM " + BaseRoleTable.TableName + " WHERE (" + BaseRoleTable.TableName + "." + BaseRoleTable.FieldRealname + " LIKE '" + search + "' OR " + BaseRoleTable.TableName + "." + BaseRoleTable.FieldDescription + " LIKE '" + search + "')";
            if (!string.IsNullOrEmpty(organizeID))
            {
                string str2 = sqlQuery;
                sqlQuery = str2 + " AND (" + BaseRoleTable.TableName + "." + BaseRoleTable.FieldOrganizeID + " = '" + organizeID + "')";
            }
            sqlQuery = sqlQuery + " ORDER BY " + BaseRoleTable.FieldSortCode;
            DataTable dataTable = new DataTable(BaseStaffTable.TableName);
            base.DbHelper.Fill(dataTable, sqlQuery);
            return dataTable;
        }

        private void SetEntity(SQLBuilder sqlBuilder, BaseRoleEntity roleEntity)
        {
            sqlBuilder.SetValue(BaseRoleTable.FieldOrganizeID, roleEntity.OrganizeID);
            sqlBuilder.SetValue(BaseRoleTable.FieldRealname, roleEntity.Realname);
            sqlBuilder.SetValue(BaseRoleTable.FieldCategory, roleEntity.Category);
            sqlBuilder.SetValue(BaseRoleTable.FieldEnabled, roleEntity.Enabled ? 1 : 0);
            sqlBuilder.SetValue(BaseRoleTable.FieldSortCode, roleEntity.SortCode);
            sqlBuilder.SetValue(BaseRoleTable.FieldDescription, roleEntity.Description);
        }

        public int Update(BaseRoleEntity roleEntity, out string statusCode)
        {
            int num = 0;
            if (DbLogic.IsModifed(base.DbHelper, BaseRoleTable.TableName, roleEntity.ID, roleEntity.ModifyUserID, roleEntity.ModifyDate))
            {
                statusCode = StatusCode.ErrorChanged.ToString();
                return num;
            }
            if (this.Exists(BaseRoleTable.FieldRealname, roleEntity.Realname, roleEntity.ID))
            {
                statusCode = StatusCode.ErrorFullNameExist.ToString();
                return num;
            }
            num = this.UpdateEntity(roleEntity);
            if (num == 1)
            {
                statusCode = StatusCode.OKUpdate.ToString();
                return num;
            }
            statusCode = StatusCode.ErrorDeleted.ToString();
            return num;
        }

        public int UpdateEntity(BaseRoleEntity roleEntity)
        {
            SQLBuilder sqlBuilder = new SQLBuilder(base.DbHelper);
            sqlBuilder.BeginUpdate(BaseRoleTable.TableName);
            this.SetEntity(sqlBuilder, roleEntity);
            sqlBuilder.SetValue(BaseRoleTable.FieldModifyUserID, base.UserInfo.ID);
            sqlBuilder.SetDBNow(BaseRoleTable.FieldModifyDate);
            sqlBuilder.SetWhere(BaseRoleTable.FieldID, roleEntity.ID);
            return sqlBuilder.EndUpdate();
        }

        public static BaseRoleDao Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (locker)
                    {
                        if (instance == null)
                        {
                            instance = new BaseRoleDao();
                        }
                    }
                }
                return instance;
            }
        }
    }
}